package com.tmp.alarm.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 *
 */
public class PropertiesUtil {

    private  final  static Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class);
    public  static Map<String,String> PROPERTISMAP = null;

    /**
     * 获取resources下的config.properties文件
     * @return
     */
    public static Map<String,String> fethPropertiesConfig(){

        LOGGER.info("system path === "+System.getProperty("user.dir"));
        Properties prop = new Properties();
        Map<String,String> propMap = new HashMap<String, String>();
        //读取属性文件a.properties
        try {
//            String propPath = System.getProperty("user.dir")+ File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+ "system.properties";
            String propPath = System.getProperty("user.dir")+File.separator+"system.properties";
            LOGGER.info("properties abs path = ==== "+propPath);

//            InputStream in = new BufferedInputStream(new FileInputStream(propPath));
            InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("system.properties");

            prop.load(in);     ///加载属性列表
            Iterator<String> it=prop.stringPropertyNames().iterator();
            while(it.hasNext()){
                String key=it.next();
                propMap.put(key,prop.getProperty(key));
                LOGGER.info(key+"="+prop.getProperty(key));
            }

            PROPERTISMAP = propMap;
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  PROPERTISMAP;
    }

    public static String get(String key){

        if(PROPERTISMAP !=null){
            return  PROPERTISMAP.get(key);
        }else{
            fethPropertiesConfig();
            return  PROPERTISMAP.get(key);
        }
    }
}
